/**
 * 启动我们的后端项目
 */
import "reflect-metadata";
import { Init } from './init'
import { useKoaServer,useContainer, Action } from 'routing-controllers';
import { join } from 'path';
import { Container } from 'typedi';
import { AuthorizationChecker } from '@/utils/authorizationChecker';

class App extends Init {
    app: typeof this.koa;


    authorizationChecker: AuthorizationChecker = new AuthorizationChecker();

    constructor() {
        super()

        useContainer(Container);
        this.app = useKoaServer(this.koa, {
            authorizationChecker: this.authorizationChecker.check.bind(this.authorizationChecker),
            cors: this.config.cors.configCors,
            validation: true,
            classTransformer: true,
            controllers: [ `${join(__dirname, '../controller/**/*{.js,.ts}')}` ]
        })
        
        this.run()
    }

    run() {
        this.app.listen(this.config.port, () => {
            console.log( `
            👏  博客后端接口 👏
            👉  1：PC官网: http://127.0.0.1:${this.config.port}/
            👉  2：Admin后台: xxxxx
            `);  
        })

        this.app.on('error', (err, ctx) => {
            console.error('server error', err, ctx)
        });
    }
}

new App